﻿using HCQ2_Common.SQL;
using HCQ2_IDAL;
using HCQ2_Model;
using HCQ2_Model.VLine.InputDto._0410;
using HCQ2_Model.VRDD.InputDto._20220620;
using HCQ2_Model.VRDD.OutputDto._20220620;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HCQ2_DAL_MSSQL
{
    public partial class T_VRAreaInfoDAL : IT_VRAreaInfoDAL
    {
        /// <summary>
        /// 获取行政区域信息
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        public List<OutputAreainfo> GetAreainfo(InputAreaInfo param)
        {
            sb.Clear();
            sb.Append($@"with aa as(
                select UPPER(LEFT(jpSign,1)) AS areaLetter,*
                from T_VRAreaInfo where area_id={param.areaID}
                union all 
                select UPPER(LEFT(bb.jpSign,1)) AS areaLetter,bb.*
                from T_VRAreaInfo bb 
                inner join aa on bb.area_id=aa.area_pid)
                select areaLetter,area_id as areaID,area_name as areaName ,area_code as areaCode ,deep,jpSign,area_path as areaPath ,area_url as areaUrl  
                from aa  order by deep;");
            return SqlHelper.ExecuteList<OutputAreainfo>(sb.ToString());
        }
        /// <summary>
        /// 获取店铺详情
        /// </summary>
        /// <param name="param"></param>
        /// <param name="sotreType"></param>
        /// <returns></returns>

        public OutputGetStoreDetail GetStoreDetail(InputGetStoreDetail param, string sotreType, int owner_id, int user_id)
        {
            sb.Clear();
            sb.Append($@"select founder_id,folder_id as folderID,folder_name as folderName,folder_path as folderPath,folder_pid as folderPid,
                folderPname=(select folder_name from T_DocumentFolder where folder_id=f.folder_pid),
                folderPpath=((select folder_path from T_DocumentFolder where folder_id=f.folder_pid)),
                folder_image as folderImg,folder_note as folderNote,sortType,statr_partner as enablePartner,bs_type as bsType,addr as bsAddress,
                dealNote=(select deal_note from T_VRFounderDeal where folder_id=f.folder_id),
                dealNoteRoot=(select deal_note from T_VRFounderDeal where folder_id=(SELECT TOP 1 a.folder_id FROM dbo.T_DocumentFolder a INNER JOIN
                dbo.T_DocumentFolder b ON b.folder_path LIKE a.folder_path+'%' AND b.folder_id=f.folder_id ORDER BY a.folder_path)),
                roleStr=(SELECT stuff((select ',' + store_type from dbo.T_VRDDStoreTypeUser WHERE store_id = f.folder_id AND 
				dd_id={user_id} for xml path('')), 1, 1, '') FROM dbo.T_VRDDStoreTypeUser a WHERE store_id=f.folder_id AND dd_id={user_id}
				GROUP BY a.store_id),");
            //是否是创始人
            if (sotreType == "owner")
            {
                //创始人店铺
                sb.Append($@"focus_num as holdNum,star_num as starNum,
                    ownerID=(select user_guid from T_User where user_id=f.founder_id), founder_name as ownerName,
                isHold=(SELECT CAST(COUNT(0) AS BIT) FROM T_VRUserFolder WHERE folder_id=f.folder_id AND folder_type=0 AND user_guid='{param.userid}'),
                isStar=(SELECT CAST(COUNT(0) AS BIT) FROM T_VRUserFolderStar WHERE folder_id=f.folder_id AND folder_type=0 AND user_guid='{param.userid}'),
                    ownerPhone=(select user_phone from T_User where user_id=f.founder_id),
                    ownerImg=(select user_img from T_User where user_id=f.founder_id) ");
            }
            else
            {
                //合伙人店铺
                sb.Append($@"holdNum=(select focus_num from T_VRFounderApply where folder_id=f.founder_id),
                    starNum=(select star_num from T_VRFounderApply where folder_id=f.founder_id),
                isHold=(SELECT CAST(COUNT(0) AS BIT) FROM T_VRUserFolder WHERE folder_id=f.folder_id AND folder_type=1 AND user_guid='{param.userid}'),
                isStar=(SELECT CAST(COUNT(0) AS BIT) FROM T_VRUserFolderStar WHERE folder_id=f.folder_id AND folder_type=1 AND user_guid='{param.userid}'),
                    ownerID=(select user_guid from  T_User where folder_id=f.folder_id and user_id={owner_id}),
                    ownerName=(select apply_name from T_VRFounderApply where folder_id=f.folder_id and user_id={owner_id}),
                    ownerPhone=(select user_phone from T_User where user_id={owner_id}),
                    ownerImg=(select user_img from T_User where user_id={owner_id}) ");
            }
            sb.Append($@" from T_DocumentFolder f where folder_id={param.folderID}");
            return SqlHelper.ExecuteList<OutputGetStoreDetail>(sb.ToString())?.FirstOrDefault();
        }
        /// <summary>
        /// 店铺分类列表（店铺的第一层子目录列表）
        /// </summary>
        /// <param name="folderID"></param>
        /// <returns></returns>
        public List<folderType> GetstoreType(int folderID)
        {
            sb.Clear();
            sb.Append($@"select folder_id as folderID,folder_name as folderName,cast(folder_order as nvarchar) as folderOrder,
                folder_path as folderPath,folder_note as folderNote from T_DocumentFolder where folder_pid={folderID}");
            return SqlHelper.ExecuteList<folderType>(sb.ToString());
        }
    }
}
